<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta charset="utf-8" />
	<title></title>
	<link href="../css/bootstrap.min.css" rel="stylesheet">
	<link href="../css/bootstrap-select.css" rel="stylesheet">
	<link href="../css/font-awesome.min.css" rel="stylesheet">
	<link href="../css/JyyCommon.css" rel="stylesheet">
	<link href="../css/JyyTable.css" rel="stylesheet">
	<link href="../css/JyyTree.css" rel="stylesheet">
	<link href="../css/JyyMsgBox.css" rel="stylesheet">
	<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
	<script type="text/javascript" src="../js/jquery.form.min.js"></script> 
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-select.js"></script>
	<script type="text/javascript" src="../js/JyyCommon.js"></script>
	<script type="text/javascript" src="../js/JyyTable.js"></script>
	<script type="text/javascript" src="../js/JyyTree.js"></script>
	<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
	<script type="text/javascript" src="../js/JyyLoading.js"></script>
	<script type="text/javascript" src="../js/imp.js"></script>
	<script type="text/javascript" src="../js/xlsx.full.min.js"></script>
	<style>
	.heidenbt{
		display: none
	}
	#jyytable_page,#jyytable_size,#jyytable_skip {
		display: none;
	}
	</style>
	<script type="text/javascript">
		var tree = null;
		var table = null;
		var scheme = null;
		var treeCode = null;
		var arr = ['1点','2点','3点','4点','5点','6点','7点','8点','9点','10点','11点','12点','13点','14点','15点','16点','17点','18点','19点','20点','21点','22点','23点','24点'];
		var tableDataTmpS = '{"data":[[{"code":"nodename","name":"节点名称"},{"code":"begintime","name":"日期"},{"code":"endtime","name":"终止时间点"},{"code":"value1","name":"1点"},{"code":"value2","name":"2点"},{"code":"value3","name":"3点"},{"code":"value4","name":"4点"},{"code":"value5","name":"5点"},{"code":"value6","name":"6点"},{"code":"value7","name":"7点"},{"code":"value8","name":"8点"},{"code":"value9","name":"9点"},{"code":"value10","name":"10点"},{"code":"value11","name":"11点"},{"code":"value12","name":"12点"},{"code":"value13","name":"13点"},{"code":"value14","name":"14点"},{"code":"value15","name":"15点"},{"code":"value16","name":"16点"},{"code":"value17","name":"17点"},{"code":"value18","name":"18点"},{"code":"value19","name":"19点"},{"code":"value20","name":"20点"},{"code":"value21","name":"21点"},{"code":"value22","name":"22点"},{"code":"value23","name":"23点"},{"code":"value24","name":"24点"}],["null","12","null","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"]],"listData":{"1":[]},"hideCols":[1,3],"isShowCheckBox":true,"isShowFun":false,"editColsIndex":[],"page":{"isShowPage":true,"option":{"currentPage":1,"numberOfPages":10,"pageSize":500,"totalPages":1,"totalSize":1,"bootstrapMajorVersion":1},"pageArea":[500]}}';
		var tableDataTmpO = eval('(' + tableDataTmpS + ')');
		var tableData_= null;
		var nodeId =  getQueryString("nodeId")||'88888888888';
		var projectId =  getQueryString("projectId")||'88888888888';
		var bizType =  getQueryString("bizType")||'88888888888';
		var isCO2 =  getQueryString("isCO2")||'88888888888';
		var impFlag = false;
		$(function(){
			 if((parent.getNodeParamData('tableEdited',nodeId) && isCO2 == 0)||(parent.getNodeParamData('tableCO2Edited',nodeId) && isCO2 == 1)){
				 initEXTable();
			 }else{
				initTable();
			 }
		});
		function initTable (){
			$.when(getTableData('zhny')).done(function(tableData){
				JyyLoading.start();
				tableData_ = tableData;
					if(table == null){
						table = JyyTable.init("table", tableData_, {position:"absolute",top:"65px",left:"25px",right:"20px",bottom:"0px",overflow:"hidden"}, onTableEvent);
					}else{
						table.refresh(tableData_);
					}
					seteXdataBtTableQuery(tableData_);
					$("#table").css("display","block");
					$("#operate").css("display","block");
					$("#iframe").css("display","none");
					JyyLoading.end();
					var strName = parent.$("#tabTitle").html();
					if(strName.indexOf('价格')!=-1){
						if(bizType=="gen_fire") $("#prName").text("单位：元/MWh");
						if(bizType=="gas_source") $("#prName").text("单位：元/m³");
						if(bizType=="heat_source") $("#prName").text("单位：元/KJ");
					}else{
						if(bizType=="gen_wind" || bizType=="gen_pv" || bizType=="distributed_wind" || bizType=="distributed_pv") $("#prName").text("单位：MW");
						if(bizType=="power_load" || bizType=="power_load_st") $("#prName").text("单位：MW");
						if(bizType=="gas_load" || bizType=="gas_load_st") $("#prName").text("单位：m³");
						if(bizType=="heat_load" || bizType=="heat_load_st" || bizType=="cold_load_st") $("#prName").text("单位：KJ");
						if(bizType=="gen_fire") {
							if(isCO2 == 1) $("#prName").text("单位：tCO2/MWh");
						}
						if(bizType=="gas_source") {
							if(isCO2 == 1) $("#prName").text("单位：tCO2/m³");
						}
						if(bizType=="heat_source") {
							if(isCO2 == 1) $("#prName").text("单位：tCO2/GJ");
						}
					}
				});
		}
		function seteXdataBtTableQuery(table){
			var tableData = table.data;
		 	var tab =[];
			for(var i = 1;i<tableData.length;i++){
				var l = {};
					l['日期'] = table.data[i][1];
				for(var r = 0;r<arr.length;r++){
					l[arr[r]] = tableData[i][r+3];
				}
				tab.push(l);
			}
   			parent.editNodeFlag  = true;
   			if(isCO2 == 0){
   				parent.setNodeParamData('eXdata',JSON.stringify(tab),nodeId);
   			} else {
   				parent.setNodeParamData('eXCO2data',JSON.stringify(tab),nodeId);
   			}
		}
		function initEXTable (){
			var tab = "";
   			if(isCO2 == 0){
   				tab = parent.getNodeParamData('eXdata',nodeId);
   			} else {
   				tab = parent.getNodeParamData('eXCO2data',nodeId);
   			}
		  	tab = eval('(' + tab + ')');
  	 		var dtmp =[];
   			dtmp.push(tableDataTmpO.data[0]);
			  for(var i = 0;i<tab.length;i++){
				  var ax = ['null'];
				      ax.push(tab[i][Object.keys(tab[i])[0]]);
				      ax.push('null')
				  for(var r = 1;r<Object.keys(tab[i]).length;r++){
					  ax.push(tab[i][Object.keys(tab[i])[r]]);
				  }
			      dtmp.push(ax);
			  }
			 
			  	tableDataTmpO.data = dtmp;
			  	tableData_ = tableDataTmpO;
				table = JyyTable.init("table", tableData_, {position:"absolute",top:"65px",left:"25px",right:"20px",bottom:"0px",overflow:"hidden"}, onTableEvent);
				 
				$("#table").css("display","block");
				$("#operate").css("display","block");
				$("#iframe").css("display","none");
				JyyLoading.end();
				var strName = parent.$("#tabTitle").html();
				if(strName.indexOf('价格')!=-1){
					if(bizType=="gen_fire") $("#prName").text("单位：元/MWh");
					if(bizType=="gas_source") $("#prName").text("单位：元/m³");
					if(bizType=="heat_source") $("#prName").text("单位：元/KJ");
				}else{
					if(bizType=="gen_wind" || bizType=="gen_pv" || bizType=="distributed_wind" || bizType=="distributed_pv") $("#prName").text("单位：MW");
					if(bizType=="power_load" || bizType=="power_load_st") $("#prName").text("单位：MW");
					if(bizType=="gas_load" || bizType=="gas_load_st") $("#prName").text("单位：m³");
					if(bizType=="heat_load" || bizType=="heat_load_st" || bizType=="cold_load_st") $("#prName").text("单位：KJ");
					if(bizType=="gen_fire") {
						if(isCO2 == 1) $("#prName").text("单位：tCO2/MWh");
					}
					if(bizType=="gas_source") {
						if(isCO2 == 1) $("#prName").text("单位：tCO2/m³");
					}
					if(bizType=="heat_source") {
						if(isCO2 == 1) $("#prName").text("单位：tCO2/GJ");
					}
				}
			 
		}
		function getQueryString (name){
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			return r ? unescape(r[2]) : null;
		}
		
		function onTableEvent(type, value){
			console.log("onTableEvent");
		 	var lis = [];
			if(type == "insertBefore"){
				console.log("insertBefore");
				//核查数据正确性
				for(var a = 0 ; a < value[0].length; a++){
					if(table.config.hideCols.indexOf(a) <= -1){
						if(a==2){
							if(!JyyCommon.isDateExp(value[1][a],'yyyymmdd')){
								JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]应为'yyyymmdd'格式的日期",type:"warning"}).on(function (e) {});
								return false;
							}
						}else{
							if(JyyCommon.isEmpty(value[1][a])){
								JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
								return false;
							}else if((!JyyCommon.isObject(value[1][a]) && value[1][a].indexOf("请选择")>-1) || value[1][a] == " "){
								JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]下拉框未做选择",type:"warning"}).on(function (e) {});
								return false;
							}else if(JyyCommon.isObject(value[1][a]) && JyyCommon.isEmpty(value[1][a].name)){
								JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
								return false;
							}
						}
					}
					lis.push();
				}
				return true;
			} else if(type == "data"){
   	   			if(isCO2 == 0){
   					parent.setNodeParamData('tableEdited',true,nodeId);
   	   			} else {
   	           		parent.setNodeParamData('tableCO2Edited',true,nodeId);
   	   			}
   	   			for(var i = 1;i < value.length;i++) {
	   	   			var dateText =  value[i][2];
	   				var tab = "";
		   			if(value[i][0]=='update'){
		   	   			if(isCO2 == 0){
		   	   				tab = parent.getNodeParamData('eXdata',nodeId);
		   	   			} else {
		   	   				tab = parent.getNodeParamData('eXCO2data',nodeId);
		   	   			}
					  	tab = eval('(' + tab + ')');
						  for(var j = 0;j<tab.length;j++){
							  if(tab[j]['日期']==dateText){
								  for(var r = 0;r<value[i].length;r++){
									  tab[j][arr[r]] = value[i][r+4];
								  }
							  }
						  }
		   			}else if(value[i][0]=='delete'){
		   	   			if(isCO2 == 0){
		   	   				tab = parent.getNodeParamData('eXdata',nodeId);
		   	   			} else {
		   	   				tab = parent.getNodeParamData('eXCO2data',nodeId);
		   	   			}
					  	tab = eval('(' + tab + ')');
		   			 for(var j = 0;j<tab.length;j++){
						  if(tab[j]['日期']==dateText){
							   tab.splice(j,1)
						  }
					  }
		   			}else if(value[i][0]=='insert'){
		   	   			if(isCO2 == 0){
		   	   				tab = parent.getNodeParamData('eXdata',nodeId);
		   	   			} else {
		   	   				tab = parent.getNodeParamData('eXCO2data',nodeId);
		   	   			}
		   				if(tab){
						  	tab = eval('(' + tab + ')');
		   				}else{
		   					tab = [];
		   				}
		   				var l = {};
		   				l['日期'] =dateText;
		   				for(var j = 0;j<arr.length;j++){
		   					l[arr[j]] = value[i][j+4];
		   				}
		   				tab.push(l);
		   			}
		   			parent.editNodeFlag  = true;
	   	   			if(isCO2 == 0){
			   			parent.setNodeParamData('eXdata',JSON.stringify(tab),nodeId);
	   	   			} else {
			   			parent.setNodeParamData('eXCO2data',JSON.stringify(tab),nodeId);
	   	   			}
   	   			}
			}  
		}
		
		//修改按钮
		function updateRow(){
			table.updateRows();
		}
		//取消修改按钮
		function cancelRow(){
			table.cancelRow();
		}
		//保存按钮
		function saveRow(){
			table.saveRow();
		}
		//下载按钮
		function download(){
			var strName = parent.$("#tabTitle").html();
			var form = $('<form action="../pv/pageExcel/downloadExcel/300300?nodeId='+nodeId+'&bizType='+bizType+'&isCO2='+isCO2+'&strName='+strName+'" style="display:none" method="post">');
			var input = $('<input type="hidden" name="projectid">');
		//	var iframe = $('<iframe name="downloadIframe" type="hidden">');
			input.attr("value",projectId);
			form.append(input);
		//	iframe.contents().find('body').append(form)
			$("body").append(form);
			form.submit();
			//iframe.remove();
			form.remove();
		}
		
		//上传按钮
		function upload(){
			var file = document.getElementById("file");
			if(file.value == ""){
				return;
			}
			JyyLoading.start();
			var fileObj = file.files[0];
			var url = '../pv/pageExcel/loadExcel/300300';
			var form = new FormData();
			form.append("file",fileObj);
			form.append("schemeid",projectId);
			form.append("nodeId",nodeId);
			form.append("bizType",bizType);
			form.append("isCO2",isCO2);
			var xhr = new XMLHttpRequest();
			xhr.open("post", url, true);
			xhr.send(form);
			xhr.onreadystatechange = function(){
			    if(xhr.readyState === 4 && xhr.status === 200){
					$.when(getTableData('zhny')).done(function(tableData){
						table.refresh(tableData);
						JyyLoading.end();
						document.getElementById("file").value=null
					});
			    }else if(xhr.readyState === 4 && xhr.status === 500) {
			    	$.when(getTableData('zhny')).done(function(tableData){
						table.refresh(tableData);
						JyyLoading.end();
						document.getElementById("file").value=null
					});
			    }
			}

		}
		//删除按钮
		function deleteRow(){
			table.deleteRows();
		}
		//新增按钮
		function addRow(){
			table.addRow();
		}
		//显示隐藏列按钮
		function colsState(e){
			var i = $(e).find('i');
			if($(i).is('.glyphicon-align-justify')){
				$(i).removeClass('glyphicon-align-justify')
				$(i).addClass('glyphicon-list');
				$(e).html($(e).html().replace("显示隐藏列", "隐藏默认列"));
				table.hideCols(false);
			}else{
				$(i).removeClass('glyphicon-list')
				$(i).addClass('glyphicon-align-justify');
				$(e).html($(e).html().replace("隐藏默认列", "显示隐藏列"));
				table.hideCols(true);
			}
		}
		//查询
		function select(){
			var value = $("#term").val();
			//var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;
			JyyLoading.start();
			$.when(getTableData('zhny', currentPage, pageSize, value)).done(function(tableData){
				table.refresh(tableData);
				JyyLoading.end();
			});
		}
		//重置
		function reset(){
			$("#term").val("");
		//	var currentPage = table.config.page.option.currentPage;
			var currentPage = 1;
			var pageSize = table.config.page.option.pageSize;
			
			JyyLoading.start();
			$.when(getTableData('zhny', currentPage, pageSize, null)).done(function(tableData){
				table.refresh(tableData);
				JyyLoading.end();
			});
		}
		
		 
		function getTableData(code, currentPage, pageSize, keyword){
			var defer = $.Deferred();
			var result = null;
			$.ajax({
				dataType: "json",
				async:true,
				data:{"pagenow":currentPage,"pagenum":pageSize,"keyword":keyword,"projectId":projectId,"nodeId":nodeId,"bizType":bizType,"isCO2":isCO2},
				url: "../pv/page/pageInfoToMeos/"+code
			}).done(function(res) {
				var sts=eval("("+res+")");
				result = sts;
				defer.resolve(sts);
			});
			return defer.promise();
			$("#file").value=null
		}
		function importfByType(e) {
			var imparr = ['日期','1点','2点','3点','4点','5点','6点','7点','8点','9点','10点','11点','12点','13点','14点','15点','16点','17点','18点','19点','20点','21点','22点','23点','24点'];
			importf(e,imparr);
			$("#file").val('');
		}
	</script>
</head>
<body style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;background-color: #f5f8ff;">
	<div id="operate" style="position: absolute;top: 0px;left: 10px;right: 0px;height: 50px;">
		<div class="col-md-12" style="position: absolute;top: 20px;padding-left: 15px;padding-right: 20px;width: 100%;">
			<button id="xinzeng" class="btn btn-info btn-sm" onclick=addRow()><i class="fa fa-plus"></i>新增</button>
			<button class="btn btn-info btn-sm" onclick=updateRow()><i class="glyphicon glyphicon-pencil"></i>修改</button>
			<button class="btn btn-info btn-sm" onclick=cancelRow()><i class="glyphicon glyphicon-share-alt"></i>取消修改</button>
			<button id="shanchu"  class="btn btn-info btn-sm" onclick=deleteRow()><i class="glyphicon glyphicon-remove"></i>删除</button>
			<button class="btn btn-info btn-sm" onclick=saveRow()><i class="glyphicon glyphicon-saved"></i>保存</button>
			<button class="btn btn-info btn-sm" id="daochu" onclick=download()><i class="glyphicon glyphicon-download"></i>导出Excel</button>
			<button class="btn btn-info btn-sm" id="daoru" onclick=$("#file").click();><i class="glyphicon glyphicon-upload"></i>导入Excel</button>
			
			<label id="prName" style="float: right;margin-top: 7px;padding: 0px;font-weight: normal;font-size: 14px;" >单位：</label>
			<input type="file" id="file" name="file" onchange="importfByType(this)" style="display:none"/>
		</div>
	</div>
	<div id="tree"></div>
	<div id="table"></div>
</body>
</html>